package com.whfc.ms.api.fmam.service.impl;

import com.whfc.common.result.PageData;
import com.whfc.common.util.DateUtil;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fmam.entity.dto.FmamWeighNoteCountDTO;
import com.whfc.fmam.entity.dto.FmamWeighNoteDTO;
import com.whfc.fmam.entity.dto.FmamWeighNoteDailyDTO;
import com.whfc.fmam.entity.dto.FmamWeighNoteDetailDTO;
import com.whfc.fmam.entity.enums.DiffResult;
import com.whfc.fmam.entity.qo.WeighNoteListQO;
import com.whfc.fmam.entity.qo.WeighNoteQO;
import com.whfc.fmam.service.FmamWeighNoteService;
import com.whfc.ms.api.fmam.cache.LocalSysDeptService;
import com.whfc.ms.api.fmam.service.WeighNoteService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2019-11-25
 */
@Service
public class WeighNoteServiceImpl implements WeighNoteService {
    @DubboReference(interfaceClass = FmamWeighNoteService.class, version = "1.0.0", timeout = 100 * 1000)
    private FmamWeighNoteService fmamWeighNoteService;

    @Autowired
    private LocalSysDeptService localSysDeptService;

    /**
     * 收发料明细台账列表
     *
     * @param request
     * @return
     */
    @Override
    public PageData<FmamWeighNoteDTO> list(WeighNoteListQO request) {
        //如果是查询全部偏差类型，则将偏差类型设置为空，因为移动收发料没有偏差类型
        List<Integer> diffResultList = request.getDiffResultList();
        DiffResult[] diffResults = DiffResult.values();
        if (diffResultList.size() == diffResults.length) {
            request.setDiffResultList(null);
        }
        PageData<FmamWeighNoteDTO> pageData = fmamWeighNoteService.list(request);
        return pageData;
    }

    /**
     * 查询磅单详情
     *
     * @param id
     * @return
     */
    @Override
    public FmamWeighNoteDetailDTO detail(Long id) {
        FmamWeighNoteDetailDTO detail = fmamWeighNoteService.detail(id);
        Integer deptId = detail.getDeptId();
        String deptName = localSysDeptService.getDeptName(deptId);
        detail.setDeptName(deptName);
        if (StringUtils.isBlank(detail.getReceiveSupplierName())) {
            detail.setReceiveSupplierName(deptName);
        }
        if (StringUtils.isBlank(detail.getSendSupplierName())) {
            detail.setSendSupplierName(deptName);
        }
        return detail;
    }

    /**
     * 获取磅单信息
     *
     * @param guid 磅单GUID
     * @return 磅单信息
     */
    @Override
    public FmamWeighNoteDetailDTO info(String guid) {
        FmamWeighNoteDetailDTO detail = fmamWeighNoteService.info(guid);
        Integer deptId = detail.getDeptId();
        String deptName = localSysDeptService.getDeptName(deptId);
        detail.setDeptName(deptName);
        return detail;
    }

    @Override
    public OssPathDTO export(WeighNoteListQO request) throws IOException {
        //如果是查询全部偏差类型，则将偏差类型设置为空，因为移动收发料没有偏差类型
        List<Integer> diffResultList = request.getDiffResultList();
        DiffResult[] diffResults = DiffResult.values();
        if (diffResultList.size() == diffResults.length) {
            request.setDiffResultList(null);
        }
        String path = fmamWeighNoteService.export(request);
        OssPathDTO data = new OssPathDTO();
        data.setPath(path);
        return data;
    }

    @Override
    public FmamWeighNoteCountDTO weighNoteCount(Integer deptId, Date date) {
        WeighNoteQO param = new WeighNoteQO();
        param.setDeptId(deptId);
        param.setStartTime(DateUtil.getDateBegin(date));
        param.setEndTime(DateUtil.getDateEnd(date));
        return fmamWeighNoteService.weighNoteCount(param);
    }

    @Override
    public PageData<FmamWeighNoteDailyDTO> daily(Integer deptId, Date time, Integer weighNoteType, Integer pageSize, Integer pageNum) {
        Date startTime = null;
        Date endTime = null;
        if (time != null) {
            startTime = DateUtil.getDateBegin(time);
            endTime = DateUtil.getDateEnd(time);
        }
        return fmamWeighNoteService.daily(deptId, weighNoteType, startTime, endTime, pageNum, pageSize);
    }
}
